<?php
/*
 * 生成pdf接口调用
 */
require_once INCLUDE_ROOT_PATH . 'php_pdf/mpdf.php';
require_once INCLUDE_ROOT_PATH . '/common.inc.php';
require_once INCLUDE_ROOT_PATH . '/file_server.class.php';
class Pdf extends File_server{

    private $mpdf;

    public function __construct()
    {

    }

    //生成pdf
    private function generate($template,$pdfname, $pdftype)
    {
        $this->mpdf = new mPDF('zh-CN');
        $this->mpdf->SetDisplayMode('fullpage');
        $this->mpdf->useAdobeCJK = true;
        $this->mpdf->WriteHTML('<pagebreak sheet-size="210mm 320mm" />');
        $this->mpdf->DeletePages(1,1);
        $this->mpdf->WriteHTML($template);
        $pdf = $this->mpdf->Output($pdfname,$pdftype);
        return $pdf;
    }

    //根据手机号生成授权书pdf并且用户签章 暂时适用于618诚信宝活动
    public function uploadpdf($template,$f_time, $pdftype,$contactid,$type = 1)
    {
        $newpdf = $this->generate($template,"con.pdf",$pdftype);//生成pdf 1模板名，2pdf名称，3pdf类型
        $time = date("Y-m",time());
        $tempPath = CREDIT_ACTIVITY_AUTHORIZATION_ROOT_PATH.$time.'/';//初始pdf路径
        if (!file_exists(CREDIT_ACTIVITY_AUTHORIZATION_ROOT_PATH)
            && !is_dir(CREDIT_ACTIVITY_AUTHORIZATION_ROOT_PATH))
        {
            mkdir(CREDIT_ACTIVITY_AUTHORIZATION_ROOT_PATH);
        }
        if(!file_exists($tempPath) && !is_dir($tempPath)){
            mkdir($tempPath);
        }

        if($type == 1)
        {
            //由于与合同没有关联 用MD5后的手机号代替
            $filename = $contactid.'_'.$f_time."_618_init.pdf"; //618征信授权书
        }

        $pathname = $tempPath.$filename;
        $result = file_put_contents($pathname, $newpdf);//临时存放

        if($result > 0)
        {
            $result = $filename;
        }else
        {
            $result = false;
            $this->update_error("文件存储目录{$tempPath}不存在或没有写入的权限");
        }
        return $result;
    }


    //上传vipfile
    public function uploadpdf2($template,$f_time,$pdftype,$cust_id,$contactid,$type = 1)
    {
        //@type
        $newpdf = $this->generate2($template,"temp.pdf",$pdftype, 0);//生成pdf 1模板名，2pdf名称，3pdf类型
        if($type == 1){
            $filename = $contactid.'_'.$f_time."_recommend_visit_record_init.pdf";//约见确认单的名称
        }
        $file_dir = PDF_ROOT_PATH . $cust_id . '/';

        if(!is_dir($file_dir)){
            mkdir($file_dir);
        }

        $pathname = $file_dir.$filename;
        $pdf_upload = file_put_contents($pathname, $newpdf);//临时存放
        if($pdf_upload){//获取url存入数据库
            $filepath = $file_dir . $filename;
            return $filename;
        }
        return false;
    }


    private function generate2($template,$pdfname, $pdftype, $template_type = 0, $contract_num="contract_num")
    {

        $this->mpdf = new mPDF('zh-CN');
        $this->mpdf->SetDisplayMode('fullpage');
        $this->mpdf->useAdobeCJK = true;
        if ($template_type == 0) {
            $this->mpdf->WriteHTML('<pagebreak sheet-size="210mm 320mm" />');
            $this->mpdf->DeletePages(1,1);
            $this->mpdf->WriteHTML($template);
        } else {
            $this->mpdf->SetImportUse();
            // 获取PDF页数
            $pagecount = $this->mpdf->SetSourceFile($template);
            // 循环每一页进行修改
            for ($pageNo = 1; $pageNo <= $pagecount; $pageNo++) {
                $this->mpdf->AddPage();
                $tplId = $this->mpdf->ImportPage($pageNo);
                $this->mpdf->UseTemplate($tplId);
                // 设置字体类型。 由于去除了/libs/php_pdf/ttfontdata中的.dat文件 导致字体设置不成功。 目前mpdf6.0暂时不支持设置字体
                //$this->mpdf->SetFont('Arial','B','12');
                // 确定位置并赋值
                $this->mpdf->WriteText(150, 10, $contract_num);
            }
        }
        $pdf = $this->mpdf->Output($pdfname,$pdftype);
        return $pdf;
    }
}
?>